﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace PMPUtils.Util
{
    public class PMPUtil
    {
        /// <summary>
        /// Return the day offset based on the sprint start date.
        /// </summary>
        /// <param name="sprintStart"></param>
        /// <returns></returns>
        public static int GetDayOffset(DateTime sprintStart)
        {

            TimeSpan ts = DateTime.Now.Subtract(sprintStart);
            int totalDays = Convert.ToInt32(ts.Days) + 1;
            if (totalDays >= 6)
            {
                totalDays -= 2;
            }
            else if (totalDays <= 1)
            {
                totalDays = 1;
            }

            return totalDays;
        }

        public static String GetLastSunday()
        {
            int decrease = 0;
            DayOfWeek weekday = DateTime.Now.DayOfWeek;

            if (weekday.Equals(DayOfWeek.Sunday))
            {
                decrease = -7;
            }
            else if (weekday.Equals(DayOfWeek.Saturday))
            {
                decrease = -6;
            }
            else if (weekday.Equals(DayOfWeek.Friday))
            {
                decrease = -5;
            }
            else if (weekday.Equals(DayOfWeek.Thursday))
            {
                decrease = -4;
            }
            else if (weekday.Equals(DayOfWeek.Wednesday))
            {
                decrease = -3;
            }
            else if (weekday.Equals(DayOfWeek.Tuesday))
            {
                decrease = -2;
            }
            else if (weekday.Equals(DayOfWeek.Monday))
            {
                decrease = -1;
            }

            DateTime today = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
            DateTime lastSunday = DateTime.Now.AddDays(decrease);

            return lastSunday.ToString("MM/dd/yyyy");
        }
    }
}
